package apputil

import (
	"encoding/binary"
	"fmt"
	"strings"
	"sync/atomic"

	"gitee.com/crack007/proxy/src/constant"
)

func CheckConnectWithDb(capabilities []byte) bool {
	return (binary.LittleEndian.Uint16(capabilities) & 8) == 8
}

func Search(input string, key string) bool {
	split := strings.Split(strings.ToLower(key), "#")
	showCmd := false
	if len(split) > 0 {
		search := strings.ToLower(input)
		for _, s2 := range split {
			if strings.Contains(search, s2) {
				showCmd = true
				break
			}
		}
	}
	return showCmd
}

func SearchCmd(input string, key string) bool {
	split := strings.Split(strings.ToLower(key), "#")
	showCmd := false
	if len(split) > 0 {
		search := strings.ToLower(input)
		for _, s2 := range split {
			if search == s2 {
				showCmd = true
				break
			}
		}
	}
	return showCmd
}

var id int64

func GetIncId() int64 {
	atomic.AddInt64(&id, 1)
	return id
}

func GetCmd(cmdText string) string {
	cmdText = strings.ToLower(cmdText)
	if cmdText == "" {
		return ""
	}
	index := strings.Index(cmdText, " ")
	if index == -1 {
		return cmdText
	}
	return cmdText[:index]
}

func AddColor(text string, color constant.Color) string {
	return string(color) + text + fmt.Sprintf("%s[%dm", constant.ESCAPE, constant.RESET)
}
